package com.dylan.datacollection.net;

import android.util.Log;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by dylan on 2018/3/30.
 */

public class HttpHelper {
    private static HttpHelper httpHelper;
    private HttpHelper() {

    }
    public static HttpHelper getInstance() {
        if (httpHelper == null) {
            httpHelper = new HttpHelper();
        }
        return httpHelper;
    }
    public void login(String userName, String password, Callback callback) {
        OkHttpUtils.get().url(HttpConfig.HOST+HttpConfig.LOGIN).addParams("username", userName).addParams("password", password).addParams("device_type", "android").build().execute(callback);
    }
    public void msgList(int page,Callback callback){
        OkHttpUtils.get().url(HttpConfig.HOST+HttpConfig.MSG_LIST).addParams("page", String.valueOf(page)).build().execute(callback);
    }
    public void checkInfo(String id,Callback callback){
        OkHttpUtils.get().url(HttpConfig.HOST+HttpConfig.CHECK_INFO).addParams("id", id).build().execute(callback);
    }
    public void jcDetail(String id,Callback callback){
        OkHttpUtils.get().url(HttpConfig.HOST+HttpConfig.JC_DETAIL).addParams("id", id).build().execute(callback);
    }
    public void changePwd(String pwd,String newPwd,String uid,Callback callback){
        OkHttpUtils.get().url(HttpConfig.HOST+HttpConfig.UP_PWD).addParams("uid", uid).addParams("password", pwd).addParams("newpassword",newPwd).build().execute(callback);
    }
    public void scoreList(int type,String id,Callback callback){
        String key = null;
        if (type==1){
            key = "ckid";
        }else if(type==2){
            key = "uid";
        }
        OkHttpUtils.get().url(HttpConfig.HOST+HttpConfig.SCORE_LIST).addParams(key, id).build().execute(callback);
    }
    public void jcList(int page,Callback callback){
        if (page==-1){
            OkHttpUtils.get().url(HttpConfig.HOST+HttpConfig.JCLIST).build().execute(callback);
        }else{
            OkHttpUtils.get().url(HttpConfig.HOST+HttpConfig.JCLIST).addParams("page", String.valueOf(page)).build().execute(callback);
        }

    }
    public void submitInfo(String total_num,String score_ave,String totalScore,String check_info_id, String observer_id, String score, File score_img,Callback callback){
//        OkHttpUtils.post().addFile("score_img",score_img.getName(),score_img).url(HttpConfig.HOST+HttpConfig.SUBMIT_INFO).addParams("check_info_id",check_info_id).addParams("observer_id",observer_id).addParams("score",score).build().execute(callback);
        Map<String,String> map = new HashMap<>();
        map.put("check_info_id",check_info_id);
        map.put("total_score",totalScore);
        map.put("observer_id",observer_id);
        map.put("total_num",total_num);
        map.put("score_ave",score_ave);
        map.put("score",score);
        Map<String,Object> header = new HashMap<>();
        header.put("score_img",score_img);
        OkHttpUtils.post().addFile("score_img",score_img.getName(),score_img).url(HttpConfig.HOST+HttpConfig.SUBMIT_INFO).params(map).build().execute(callback);

    }
    public void upload(File score_img){
//        OkHttpUtils.postFile().url(HttpConfig.HOST+HttpConfig.upload).file(score_img).build().execute(new ResponseCallback(){
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                super.onError(call, e, id);
//                Log.i("UPLOAD","response test1："+e.getMessage());
//            }
//
//            @Override
//            public void onResponse(Object response, int id) {
//                super.onResponse(response, id);
//                Log.i("UPLOAD","response test1："+response.toString());
//            }
//        });
        Log.i("LOGIN","response start:"+score_img.length()+";file name:"+score_img.getName());
        OkHttpUtils.post().addFile("score_img",score_img.getName(),score_img).url(HttpConfig.HOST+HttpConfig.upload).build().execute(new ResponseCallback(){
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                Log.i("UPLOAD","response error test："+e.getMessage());
            }

            @Override
            public void onResponse(Object response, int id) {
                super.onResponse(response, id);
                Log.i("UPLOAD","response success test："+response.toString());
            }
        });
    }
}
